/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Utility;

import CRUD.BanHome;
import CRUD.CategoryHome;
import CRUD.ForumHome;
import CRUD.MemberHome;
import CRUD.PostHome;
import CRUD.SuspendHome;
import CRUD.ThreadHome;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
//import pkg.Search;
import sun.misc.BASE64Encoder;
import xmlMapping.Ban;
import xmlMapping.Category;
import xmlMapping.Forum;
import xmlMapping.Member;
import xmlMapping.Post;
import xmlMapping.Suspend;
import xmlMapping.Theme;
import xmlMapping.Thread;

/**
 *
 * @author sh
 */
public class Utility {

//    public Search search(String key) {
//        //System.out.println("in search/////////");
//        Search search = new Search();
//        CategoryHome catHome = new CategoryHome();
//        ForumHome forumHome = new ForumHome();
//        ThreadHome threadHome = new ThreadHome();
//        PostHome postHome = new PostHome();
//        Iterator cat = catHome.search(key).iterator();
//        search.setCategory(cat);
//        Iterator forum = forumHome.search(key).iterator();
//        search.setForum(forum);
//        Iterator thread = threadHome.search(key).iterator();
//        search.setThread(thread);
//        Iterator post = postHome.search(key).iterator();
//        search.setPost(post);
//        return search;
////        Category category;
////        while(i.hasNext()){
////            category=(Category)i.next();
////            System.out.println("cat name:"+category.getName());
////
////        }
////        System.out.println("end of search/////////");
//    }

    public boolean addUser(Member member) {
        //boolean result=false;
        MemberHome home = new MemberHome();
        return home.persist(member);
    }

    public List getCategories() {
        CategoryHome home = new CategoryHome();
        return home.getCategories();
    }

    public List getForums(int id) {
        ForumHome home = new ForumHome();
        Category category = new Category();
        category.setCategoryId(id);
        return home.getForums(category);
    }

    public Forum getForumById(int id) {
        return (new ForumHome().findById(new Integer(id)));
    }

    public List getPosts(int id) {
        PostHome home = new PostHome();
        Thread thread = new Thread();
        thread.setThreadId(id);
        return home.getPosts(thread);
    }

    public List getPostspage(int id,int pageNumber) {
        PostHome home = new PostHome();
        Thread thread = new Thread();
        thread.setThreadId(id);
        return home.getPostsPage(thread,pageNumber);
    }

    public int getCountPosts(int thId){
         PostHome home = new PostHome();
         return home.getCount(thId);
    }

    public List getSticky(int fId) {
        ThreadHome home = new ThreadHome();
        Forum forum = new Forum();
        forum.setForumId(fId);
        return home.getSticky(forum);
    }

    public List getThreads(int fId) {
        ThreadHome home = new ThreadHome();
        Forum forum = new Forum();
        forum.setForumId(fId);
        return home.getThreads(forum);
    }

    public Member login(Member member) {
        MemberHome home = new MemberHome();
        Member mem = home.login(member);
        return mem;
    }

    public Ban checkBan(int mId) {
        BanHome home = new BanHome();
        return home.findById(mId);
    }

    public Suspend checkSuspend(int mId) {
        SuspendHome home = new SuspendHome();
        Member mem = new MemberHome().findById(mId);//new Member();
        //mem.setMemberId(mId);
//        Suspend sus=new Suspend();
//        sus.setMemberByMemberId(mem);
        Iterator i = home.checkSuspend(mem).iterator();//home.findByExample(sus).iterator();
        Suspend suspend = null;
        while (i.hasNext()) {
            suspend = (Suspend) i.next();
            if (suspend.getExpiredate().after(new Date())) {
                return suspend;
            }
        }
        return null;

    }

    public boolean checkThreadLocked(int threadId) {
       boolean lock = false;
     ThreadOperationsUtility threadUtil = new ThreadOperationsUtility();
     xmlMapping.Thread thread=threadUtil.getThread(threadId);
     lock=thread.isLocked();
        System.out.println("this thread is locked ??"+lock);
        return lock;
    }
    public boolean checkThreadBlocked(int threadId) {
       boolean block = false;
     ThreadOperationsUtility threadUtil = new ThreadOperationsUtility();
     xmlMapping.Thread thread=threadUtil.getThread(threadId);
     block=thread.isBlocked();
     System.out.println("this thread is blocked ??"+block);
        return block;
    }


    /*public static void main(String [] args){
    Utility utility=new Utility();
    ///////////search//////////
    Search sear=utility.search("ma");
    Iterator i= sear.getThread();
    while(i.hasNext()){
    System.out.println("search thread:"+((Thread)i.next()).getTitle());
    }
    ////member////
    /*Member member=new Member();
    member.setUserName("new");
    member.setPassword("asas");
    member.setEmail("sdsf");
    Theme theme=new Theme();
    theme.setThemeId(1);
    member.setTheme(theme);
    member.setType("member");
    if(utility.addUser(member)){
    System.out.println("add done /////");
    }else{
    System.out.println("add fail /////");
    }*/
    ////////////////category///////////////
        /*Iterator veccat=utility.getCategories().iterator();
    while(veccat.hasNext()){
    System.out.println("cat name:"+((Category)veccat.next()).getName());
    }*/
    //////////////forum/////////
       /* Iterator vecforum=utility.getForums(1).iterator();
    while(vecforum.hasNext()){
    System.out.println("forum name:"+((Forum)vecforum.next()).getName());
    }*/
    ////////////////post///////
       /*Iterator vecpost=utility.getPosts(1).iterator();
    System.out.println("//////////////////post title:////////////////////");
    while(vecpost.hasNext()){
    System.out.println("//////////////////post title:"+((Post)vecpost.next()).getTitle());
    }*/
    /////////sticky////////
        /* Iterator vecthread=utility.getSticky(1).iterator();
    System.out.println("//////////////////post title33333:////////////////////"+vecthread);
    while(vecthread.hasNext()){
    System.out.println("//////////////////post title33333:///////////////6666/////"+vecthread);
    System.out.println("///////////////////sticky thread title:"+((Thread)vecthread.next()).getTitle());
    }*/
    /////////////get thread//////////
        /* Iterator vecthread=utility.getThreads(1).iterator();
    while(vecthread.hasNext()){
    System.out.println("thread name:"+((Thread)vecthread.next()).getTitle());
    }*/
    /////////member////////
       /*  Member mm=new Member();
    mm.setUserName("aaaaa");
    mm.setPassword("asas");
    Member check=utility.login(mm);
    if(check !=null){
    System.out.println("login done");
    }else{
    System.out.println("login fail");
    }*/
    // }
//    public static void main(String [] args){
//         Utility utility=new Utility();
//         //utility.checkBan(1);
//         Suspend sss=utility.checkSuspend(3);
//         if(sss!=null){
//             System.out.println("sus id :"+sss.getSuspendId());
//         }else{
//             System.out.println("sus id :nnnnnnnnnnnnnn");
//         }
//
//
//    }
    public String encrypt(String passswd) throws NoSuchAlgorithmException
    {
          MessageDigest md = MessageDigest.getInstance("MD5");

byte[] buffer = new byte[40];

  buffer=passswd.getBytes();
  md.update(buffer, 0, buffer.length);
byte[] raw = md.digest();
String en=new String(raw);  
System.out.println("using string "+en);
// Print out the digest in base64.
BASE64Encoder encoder = new BASE64Encoder();
String base64 = encoder.encode(raw);
return base64;
    }
    
    
}
